/*
 * StrahlentherapieDetectorConstruction.cc
 *
 *  Created on: Oct 1, 2010
 *      Author: pwagner
 */

#include "StrahlentherapieDetectorConstruction.hh"

#include "G4NistManager.hh"
#include "G4SDManager.hh"

#include "G4Material.hh"

#include "G4Box.hh"
#include "G4Ellipsoid.hh"
#include "G4Tubs.hh"
#include "G4Orb.hh"
#include "G4UnionSolid.hh"
#include "G4SubtractionSolid.hh"
#include "G4RotationMatrix.hh"
#include "G4LogicalVolume.hh"
#include "G4ThreeVector.hh"
#include "G4PVPlacement.hh"
#include "G4VisAttributes.hh"

#include "globals.hh"
#include "G4ios.hh"

// Sensitive detectors
#include "G4MultiFunctionalDetector.hh"

#include "StrahlentherapieROGHead.hh"
#include "StrahlentherapieROGDetectorPlate.hh"

#include "StrahlentherapiePSEnergyDep3D.hh"
#include "StrahlentherapiePSDoseDep3D.hh"

// Auskommentieren wenn nicht gewünscht
//#define Detektorplatte 1

StrahlentherapieDetectorConstruction::StrahlentherapieDetectorConstruction()
{}

StrahlentherapieDetectorConstruction::~StrahlentherapieDetectorConstruction()
{}

/**
 * Wird automatisch von Geant aufgerufen, sobald die Konstruktion gestartet werden soll.
 * Ruft alle Methoden zur Konstruktion von Material, logischen und physischen Volumen auf.
 *
 * @return physische Welt-Volumen
 */
G4VPhysicalVolume* StrahlentherapieDetectorConstruction::Construct()
{
	SetRelativePositioning();

	ConstructMaterialsAndLogicals();

	SetVisualAttributes();

	ConstructPhysicalVolumes();

	SetSensitiveDetectors();

	return worldVolume_phys;
}

void StrahlentherapieDetectorConstruction::SetRelativePositioning()
{
	// Head
	skinPosition_x = 0.*cm;
	skinPosition_y = 0.*cm;
	skinPosition_z = 0.*cm;

	// Muscle
	neckPosition_x = 0.*cm;
	neckPosition_y = -10.9*cm;
	neckPosition_z = 5.5*cm;

	// Afterbrain
	afterbrainPosition_x = 0.*cm;
	afterbrainPosition_y = -9.8*cm;
	afterbrainPosition_z = 5.5*cm;

	// Eyes
	eyePosition_dx = 3.*cm;
	eyePosition_y = -1.*cm;
	eyePosition_z = -14.*cm;

	// Tumor
	tumorPosition_x = 0.*cm;
	tumorPosition_y = -1.*cm;
	tumorPosition_z = 5.*cm;

	// Head absolute
	headAbsolutePosition_x = 0.*m;
	headAbsolutePosition_y = 0.*m;
	headAbsolutePosition_z = 1.*m;

	// Lead plate
	lead_plate_x = 0.*m;
	lead_plate_y = 0.*cm;
	lead_plate_z = 0.5*m;
}

/**
 * Erstellt Materialien und logische Volumina.
 */
void StrahlentherapieDetectorConstruction::ConstructMaterialsAndLogicals()
{	
	// ----------------------------- Materials --------------------------------
	G4NistManager* manager = G4NistManager::Instance();

	// Air
	air = manager->FindOrBuildMaterial("G4_AIR");
	// Water
	water = manager->FindOrBuildMaterial("G4_WATER");
	// Lead
	lead = manager->FindOrBuildMaterial("G4_LEAD_OXIDE");
	// Vacuum
	//vacuum = manager->FindOrBuildMaterial("G4_Galactic");
	// Skin
	skin = manager->FindOrBuildMaterial("G4_SKIN_ICRP");
	// Bone
	bone = manager->FindOrBuildMaterial("G4_BONE_COMPACT_ICRU");
	bone_cortical = manager->FindOrBuildMaterial("G4_BONE_CORTICAL_ICRP");
	//bone_b100 = manager->FindOrBuildMaterial("G4_B-100_BONE");
	// Brain
	brain = manager->FindOrBuildMaterial("G4_BRAIN_ICRP");
	// Eyelens
	//eyelens = manager->FindOrBuildMaterial("G4_EYE_LENS_ICRP");
	// Muscle
	muscle = manager->FindOrBuildMaterial("G4_MUSCLE_WITHOUT_SUCROSE");
	//muscle_skeletal = manager->FindOrBuildMaterial("G4_MUSCLE_SKELETAL_ICRP");
	/*
	 * Material: Tumor
	 * Elemente und die Fraktionierung sind dem G4_BRAIN_ICRP gleich,
	 * allerdings besitzt der Tumor eine hoehere Dichte.
	 */
	// Elements for tumor
	G4String name;
	G4Material* hydrogen = manager->FindOrBuildMaterial(name="G4_H");
	G4Material* carbon = manager->FindOrBuildMaterial(name="G4_C");
	G4Material* nitrogen = manager->FindOrBuildMaterial(name="G4_N");
	G4Material* oxygen = manager->FindOrBuildMaterial(name="G4_O");
	G4Material* sodium = manager->FindOrBuildMaterial(name="G4_Na"); // Natrium
	G4Material* magnesium = manager->FindOrBuildMaterial(name="G4_Mg");
	G4Material* phosphorus = manager->FindOrBuildMaterial(name="G4_P");
	G4Material* sulphur = manager->FindOrBuildMaterial(name="G4_S");
	G4Material* chlorine = manager->FindOrBuildMaterial(name="G4_Cl");
	G4Material* potassium = manager->FindOrBuildMaterial(name="G4_K"); // Kalium
	G4Material* calcium = manager->FindOrBuildMaterial(name="G4_Ca");
	G4Material* iron = manager->FindOrBuildMaterial(name="G4_Fe");
	G4Material* zinc = manager->FindOrBuildMaterial(name="G4_Zn");

	// Build tumor material
	G4double density, fractionmass;
	G4int ncomponents;
	tumor = new G4Material(name="MT_TUMOR", density=1.2*g/cm3, ncomponents=13);
	tumor->AddMaterial(hydrogen, fractionmass=0.110667);
	tumor->AddMaterial(carbon, fractionmass=0.12542);
	tumor->AddMaterial(nitrogen, fractionmass=0.01328);
	tumor->AddMaterial(oxygen, fractionmass=0.737723);
	tumor->AddMaterial(sodium, fractionmass=0.00184);
	tumor->AddMaterial(magnesium, fractionmass=0.00015);
	tumor->AddMaterial(phosphorus, fractionmass=0.00354);
	tumor->AddMaterial(sulphur, fractionmass=0.00177);
	tumor->AddMaterial(chlorine, fractionmass=0.00236);
	tumor->AddMaterial(potassium, fractionmass=0.0031);
	tumor->AddMaterial(calcium, fractionmass=9.e-5);
	tumor->AddMaterial(iron, fractionmass=5.e-5);
	tumor->AddMaterial(zinc, fractionmass=1.e-5);

	neutron_star = new G4Material(name="neutron_star", density=100*kg/cm3, ncomponents=1);
	neutron_star->AddMaterial(lead, fractionmass=1);

	G4cout << "Materials successful constructed!" << G4endl;

	// --------------------------- Logical Volumes ------------------------------
	// worldVolume
	G4double extent_x = 2.0*m;
	G4double extent_y = 2.0*m;
	G4double extent_z = 2.0*m;
	G4VSolid* worldVolume_box = new G4Box("worldVolume_box", extent_x, extent_y, extent_z);
	worldVolume_log = new G4LogicalVolume(worldVolume_box, air, "worldVolume_log");

	// Lead plate
	G4double lead_plate_extent_x = 22.5*cm;
	G4double lead_plate_extent_y = 22.5*cm;
	G4double lead_plate_extent_z = 2.5*cm;
	G4VSolid* lead_plate_box = new G4Box("lead_plate_box", lead_plate_extent_x, lead_plate_extent_y, lead_plate_extent_z);

	G4double lead_hole_innerRadius = 0.*cm;
	G4double lead_hole_outerRadius = 2.1*cm;
	G4double lead_hole_extent_z = 2.6*cm;
	G4double lead_hole_startingRadians = 0.;
	G4double lead_hole_sweepingRadians = 360.*deg;
	G4VSolid* lead_hole_tubs = new G4Tubs("lead_hole_tubs", lead_hole_innerRadius, lead_hole_outerRadius, lead_hole_extent_z, lead_hole_startingRadians, lead_hole_sweepingRadians);

	G4RotationMatrix* xRot = new G4RotationMatrix();
	xRot->rotateX(90.*degree);
	G4ThreeVector trans(0., 0., 0.);

	G4SubtractionSolid* lead_plate_subtraction = new G4SubtractionSolid("lead_plate-hole", lead_plate_box, lead_hole_tubs);
	lead_plate_log = new G4LogicalVolume(lead_plate_subtraction, lead, "lead_plate_log");
	
	// Detektorplatte zur Validierung der Bleiplatte
#ifdef Detektorplatte
	G4VSolid* detector_box = new G4Box("detector_box", 20.*cm, 20.*cm, 1.*cm);
	detector_log = new G4LogicalVolume(detector_box, neutron_star, "detector_log");
#endif	

	// Head
	G4double head_extent_x = 20.*cm;
	G4double head_extent_y = 20.*cm;
	G4double head_extent_z = 20.*cm;
	G4VSolid* head_box = new G4Box("head_box", head_extent_x, head_extent_y, head_extent_z);
	head_log = new G4LogicalVolume(head_box, air, "head_log");

	// Skin
	G4double  skin_ellipsoid_pxSemiAxis = 10.*cm;
	G4double  skin_ellipsoid_pySemiAxis = 10.*cm;
	G4double  skin_ellipsoid_pzSemiAxis = 15.*cm;
	G4double  skin_ellipsoid_pzBottomCut = 0;
	G4double  skin_ellipsoid_pzTopCut = 0;
	G4VSolid* skin_ellipsoid = new G4Ellipsoid("skin_ellipsoid", skin_ellipsoid_pxSemiAxis, skin_ellipsoid_pySemiAxis, skin_ellipsoid_pzSemiAxis, skin_ellipsoid_pzBottomCut, skin_ellipsoid_pzTopCut);

	G4double skin_tubs_innerRadius = 0.*cm;
	G4double skin_tubs_outerRadius = 5.5*cm;
	G4double skin_tubs_extent_z = 8.*cm;
	G4double skin_tubs_startingRadians = 0.;
	G4double skin_tubs_sweepingRadians = 360.*deg;
	G4VSolid* skin_tubs = new G4Tubs("skin_tubs", skin_tubs_innerRadius, skin_tubs_outerRadius, skin_tubs_extent_z, skin_tubs_startingRadians, skin_tubs_sweepingRadians);

	// Muscles
	G4double muscle_neck_tubs_innerRadius = 3.*cm;
	G4double muscle_neck_tubs_outerRadius = 5.4*cm;
	G4double muscle_neck_tubs_extent_z = 9*cm;
	G4double muscle_neck_tubs_startingRadians = 0.;
	G4double muscle_neck_tubs_sweepingRadians = 360.*deg;
	G4VSolid* muscle_neck_tubs = new G4Tubs("muscle_neck_tubs", muscle_neck_tubs_innerRadius, muscle_neck_tubs_outerRadius, muscle_neck_tubs_extent_z, muscle_neck_tubs_startingRadians, muscle_neck_tubs_sweepingRadians);

	// Bones
	G4double  bone_head_ellipsoid_pxSemiAxis = 9.9*cm;
	G4double  bone_head_ellipsoid_pySemiAxis = 14.9*cm;
	G4double  bone_head_ellipsoid_pzSemiAxis = 9.9*cm;
	G4double  bone_head_ellipsoid_pzBottomCut = 0;
	G4double  bone_head_ellipsoid_pzTopCut = 0;
	G4VSolid* bone_head_ellipsoid = new G4Ellipsoid("bone_head_ellipsoid", bone_head_ellipsoid_pxSemiAxis, bone_head_ellipsoid_pySemiAxis, bone_head_ellipsoid_pzSemiAxis, bone_head_ellipsoid_pzBottomCut, bone_head_ellipsoid_pzTopCut);

	// Afterbrain
	G4double afterbrain_tubs_innerRadius = 0.*cm;
	G4double afterbrain_tubs_outerRadius = 3.*cm;
	G4double afterbrain_tubs_extent_z = 10.*cm;
	G4double afterbrain_tubs_startingRadians = 0.;
	G4double afterbrain_tubs_sweepingRadians = 360.*deg;
	G4VSolid* afterbrain_tubs = new G4Tubs("afterbrain_tubs", afterbrain_tubs_innerRadius, afterbrain_tubs_outerRadius, afterbrain_tubs_extent_z, afterbrain_tubs_startingRadians, afterbrain_tubs_sweepingRadians);
	afterbrain_log = new G4LogicalVolume(afterbrain_tubs, bone_cortical, "afterbrain_log");

	// Brain
	G4double  brain_head_ellipsoid_pxSemiAxis = 8.9*cm;
	G4double  brain_head_ellipsoid_pySemiAxis = 13.9*cm;
	G4double  brain_head_ellipsoid_pzSemiAxis = 8.9*cm;
	G4double  brain_head_ellipsoid_pzBottomCut = 0;
	G4double  brain_head_ellipsoid_pzTopCut = 0;
	G4VSolid* brain_head_ellipsoid = new G4Ellipsoid("brain_head_ellipsoid", brain_head_ellipsoid_pxSemiAxis, brain_head_ellipsoid_pySemiAxis, brain_head_ellipsoid_pzSemiAxis, brain_head_ellipsoid_pzBottomCut, brain_head_ellipsoid_pzTopCut);

	// Eyes
	G4double eyeRadius = 1.5*cm;
	G4VSolid* eye_orb = new G4Orb("eye_orb", eyeRadius);
	eye_left_log = new G4LogicalVolume(eye_orb, water, "eye_left_log");
	eye_right_log = new G4LogicalVolume(eye_orb, water, "eye_right_log");

	// Muscle
	G4ThreeVector trans_muscle_brain(0.*cm, 5.5*cm, 10.9*cm);
	G4SubtractionSolid* muscle_brain_subtraction = new G4SubtractionSolid("muscle-brain", muscle_neck_tubs, brain_head_ellipsoid, 0, trans_muscle_brain);
	muscle_neck_log = new G4LogicalVolume(muscle_brain_subtraction, muscle, "muscle_log");

	// Skin
	G4ThreeVector trans_skin_eye_left(eyePosition_dx, -1.*cm, -14.*cm);
	G4SubtractionSolid* skin_eye_left_subtraction = new G4SubtractionSolid("skin-eye_left", skin_ellipsoid, eye_orb, false, trans_skin_eye_left);
	G4ThreeVector trans_skin_eye_right(-eyePosition_dx, -1.*cm, -14.*cm);
	G4SubtractionSolid* skin_eye_right_subtraction = new G4SubtractionSolid("skin-eye_right", skin_eye_left_subtraction, eye_orb, false, trans_skin_eye_right);
	G4ThreeVector trans_skin(0.*cm, -12.*cm, 5.5*cm);
	G4UnionSolid* skin_union = new G4UnionSolid("head+neck", skin_eye_right_subtraction, skin_tubs, xRot, trans_skin);
	skin_log = new G4LogicalVolume(skin_union, skin, "skin_log");

	// Bone
	G4ThreeVector trans_bone_muscle(0.*cm, -5.5*cm, -10.9*cm);
	G4SubtractionSolid* bone_muscle_subtraction = new G4SubtractionSolid("bone_head-muscle", bone_head_ellipsoid, muscle_neck_tubs, false, trans_bone_muscle);
	G4ThreeVector trans_bone_eye_left(eyePosition_dx, -1.*cm, -14.*cm);
	G4SubtractionSolid* bone_eye_left_subtraction = new G4SubtractionSolid("bone-eye_left", bone_muscle_subtraction, eye_orb, false, trans_bone_eye_left);
	G4ThreeVector trans_bone_eye_right(-eyePosition_dx, -1.*cm, -14.*cm);
	G4SubtractionSolid* bone_eye_right_subtraction = new G4SubtractionSolid("bone-eye_right", bone_eye_left_subtraction, eye_orb, false, trans_bone_eye_right);
	bone_head_log = new G4LogicalVolume(bone_eye_right_subtraction, bone, "bone_head_log");

	// Brain
	G4ThreeVector trans_brain_afterbrain(0.*cm, -5.5*cm, -9.8*cm);
	G4SubtractionSolid* brain_afterbrain_subtraction = new G4SubtractionSolid("brain-afterbrain", brain_head_ellipsoid, afterbrain_tubs, false, trans_brain_afterbrain);
	G4ThreeVector trans_brain_eye_left(eyePosition_dx, -1.*cm, -14.*cm);
	G4SubtractionSolid* brain_eye_left_subtraction = new G4SubtractionSolid("brain-eye_left", brain_afterbrain_subtraction, eye_orb, false, trans_brain_eye_left);
	G4ThreeVector trans_brain_eye_right(-eyePosition_dx, -1.*cm, -14.*cm);
	G4SubtractionSolid* brain_eye_right_subtraction = new G4SubtractionSolid("brain-eye_right", brain_eye_left_subtraction, eye_orb, false, trans_brain_eye_right);
	brain_head_log = new G4LogicalVolume(brain_eye_right_subtraction, brain, "brain_head_log");

	// Tumor
	G4double tumorRadius = 2.*cm;
	G4VSolid* tumor_orb = new G4Orb("tumor_orb", tumorRadius);
	tumor_log = new G4LogicalVolume(tumor_orb, tumor, "tumor_log");

	G4cout << "Logical volumes successful created!" << G4endl;
}

/**
 * Setzt Attribute wie Farbe fuer Volumina.
 */
void StrahlentherapieDetectorConstruction::SetVisualAttributes()
{
	// ---------------------visualization attributes ---------------------------------

	// World
	G4VisAttributes* worldVolume_Att = new G4VisAttributes(G4Colour(0.0, 0.0, 0.0));
	worldVolume_Att->SetVisibility(true);
	worldVolume_log->SetVisAttributes(worldVolume_Att);

	// Brain
	G4VisAttributes* brain_head_Att = new G4VisAttributes(G4Colour(0.75, 0.75, 0.75));
	brain_head_Att->SetVisibility(true);
	brain_head_log->SetVisAttributes(brain_head_Att);

	// Afterbrain
	G4VisAttributes* afterbrain_Att = new G4VisAttributes(G4Colour(1.0, 0.8, 0));
	afterbrain_Att->SetVisibility(true);
	afterbrain_log->SetVisAttributes(afterbrain_Att);

	// Muscle
	G4VisAttributes* muscle_neck_Att = new G4VisAttributes(G4Colour(1.0, 0.0, 0.0));
	muscle_neck_Att->SetVisibility(true);
	muscle_neck_log->SetVisAttributes(muscle_neck_Att);

	// Skin
	G4VisAttributes* skin_Att = new G4VisAttributes(G4Colour(1.0, 0.9, 0.7));
	skin_Att->SetVisibility(true);
	skin_log->SetVisAttributes(skin_Att);

	// Eyes
	G4VisAttributes* eye_Att = new G4VisAttributes(G4Colour(0.0, 0.0, 1.0));
	eye_Att->SetVisibility(true);
	eye_left_log->SetVisAttributes(eye_Att);
	eye_right_log->SetVisAttributes(eye_Att);

	// Bone
	G4VisAttributes* bone_Att = new G4VisAttributes(G4Colour(1.0, 1.0, 1.0));
	bone_Att->SetVisibility(true);
	bone_head_log->SetVisAttributes(bone_Att);

	// Tumor
	G4VisAttributes* tumor_Att = new G4VisAttributes(G4Colour(1, 1, 1));
	tumor_Att->SetVisibility(true);
	tumor_log->SetVisAttributes(tumor_Att);

	// Lead plate
	G4VisAttributes* lead_plate_Att = new G4VisAttributes(G4Colour(1, 1, 1));
	lead_plate_Att->SetVisibility(true);
	lead_plate_log->SetVisAttributes(lead_plate_Att);
}


/**
 * Erstellen der physischen Volumina.
 */
void StrahlentherapieDetectorConstruction::ConstructPhysicalVolumes()
{
	// World
	worldVolume_phys = new G4PVPlacement(0, G4ThreeVector(0., 0., 0.), worldVolume_log, "worldVolume", false, false, 0, false);

	// Skin
	skin_phys = new G4PVPlacement(0, G4ThreeVector(skinPosition_x, skinPosition_y, skinPosition_z), skin_log, "skin", head_log, false, 0, false);

	G4RotationMatrix* xRot = new G4RotationMatrix();
	xRot->rotateX(90.*degree);
	// Bone
	bone_head_phys = new G4PVPlacement(xRot, G4ThreeVector(0., 0., 0.), bone_head_log, "bone", skin_log, false, 0, false);

	// Muscles
	muscle_neck_phys = new G4PVPlacement(xRot, G4ThreeVector(neckPosition_x, neckPosition_y, neckPosition_z), muscle_neck_log, "muscle", skin_log, false, 0, false);

	// Brain
	brain_head_phys = new G4PVPlacement(0, G4ThreeVector(0., 0., 0.), brain_head_log, "brain", bone_head_log, false, 0, false);

	// Afterbrain
	afterbrain_phys = new G4PVPlacement(xRot, G4ThreeVector(afterbrainPosition_x, afterbrainPosition_y, afterbrainPosition_z), afterbrain_log, "afterbrain", skin_log, false, 0, false);

	// Eyes
	eye_left_phys = new G4PVPlacement(0, G4ThreeVector(eyePosition_dx, eyePosition_y, eyePosition_z), eye_left_log, "eye", head_log, false, 0, false);
	eye_right_phys = new G4PVPlacement(0, G4ThreeVector(-eyePosition_dx, eyePosition_y, eyePosition_z), eye_right_log, "eye", head_log, false, 0, false);

	// Tumor
	tumor_phys = new G4PVPlacement(0, G4ThreeVector(tumorPosition_x, tumorPosition_y, tumorPosition_z), tumor_log, "tumor", brain_head_log, false, 0, false);

	// Head
	head_phys = new G4PVPlacement(0, G4ThreeVector(headAbsolutePosition_x, headAbsolutePosition_y, headAbsolutePosition_z), head_log, "head", worldVolume_log, false, 0, false);

	// Lead plate
	lead_plate_phys = new G4PVPlacement(0, G4ThreeVector(lead_plate_x, lead_plate_y, lead_plate_z), lead_plate_log, "lead_plate", worldVolume_log, false, 0, false);

	// Detector Platte zur Validierung
#ifdef Detektorplatte	
	detector_phys = new G4PVPlacement(0, G4ThreeVector(lead_plate_x, lead_plate_y, lead_plate_z + 10.*cm), detector_log, "detector", worldVolume_log, false, 0, false);
#endif

	G4cout << "Physical volumes successful created" << G4endl;
}

void StrahlentherapieDetectorConstruction::SetSensitiveDetectorToLogVolAndDaughters(G4LogicalVolume * root, G4VSensitiveDetector * sd) {
	for(G4int i = 0; i < root->GetNoDaughters(); i++) {
		SetSensitiveDetectorToLogVolAndDaughters(root->GetDaughter(i)->GetLogicalVolume(), sd);
	}
	root->SetSensitiveDetector(sd);
	G4cout << "SensitiveDetector " << sd->GetFullPathName() << " set to " << root->GetName() << G4endl;
}

/**
 * Zuweisen der sensitiven Detektoren.
 */
void StrahlentherapieDetectorConstruction::SetSensitiveDetectors()
{
	// -------------------- sensitive detectors -------------------------
	G4SDManager* SDmanager = G4SDManager::GetSDMpointer();
	G4String SDname;

	SDname = "/headSD";
	G4MultiFunctionalDetector * headSD = new G4MultiFunctionalDetector(SDname);
	
	// Readout Geometry Head (Name, nDiv)
	G4int nDiv = 400;
	G4cout << "ReadOutGeometry Head - nDiv: " << nDiv << G4endl;
	StrahlentherapieROGHead* ROGHead = new StrahlentherapieROGHead("StrahlentherapieROGHead", nDiv);
	ROGHead->BuildROGeometry();
	headSD->SetROgeometry(ROGHead);

	// Scorers
	G4VPrimitiveScorer* primScorer;
	primScorer = new StrahlentherapiePSEnergyDep3D("energyDep", nDiv, nDiv, nDiv, 0, 1, 2);
	primScorer->SetMultiFunctionalDetector(headSD);
	headSD->RegisterPrimitive(primScorer);
	primScorer = new StrahlentherapiePSDoseDep3D("doseDep", nDiv, nDiv, nDiv, 0, 1, 2);
	primScorer->SetMultiFunctionalDetector(headSD);
	headSD->RegisterPrimitive(primScorer);

	SetSensitiveDetectorToLogVolAndDaughters(head_log, headSD);
	headSD->Activate(true);
	SDmanager->AddNewDetector(headSD);

#ifdef Detektorplatte
	SDname = "/detectorPlateSD";
	G4MultiFunctionalDetector * detectorSD = new G4MultiFunctionalDetector(SDname);
	
	// Readout Geometry Detector Plate (Name, nDiv)
	nDiv = 400;
	G4cout << "ReadOutGeometry Detector Plate - nDiv: " << nDiv << G4endl;
	StrahlentherapieROGDetectorPlate* ROGDetectorPlate = new StrahlentherapieROGDetectorPlate("StrahlentherapieROGDetectorPlate", nDiv);
	ROGDetectorPlate->BuildROGeometry();
	detectorSD->SetROgeometry(ROGDetectorPlate);
	
	// Scorer
	primScorer = new StrahlentherapiePSEnergyDep3D("energyDep", nDiv, nDiv, 1, 0, 1, 2);
	primScorer->SetMultiFunctionalDetector(detectorSD);
	detectorSD->RegisterPrimitive(primScorer);

	detector_log->SetSensitiveDetector(detectorSD);
	detectorSD->Activate(true);
	SDmanager->AddNewDetector(detectorSD);
#endif
}

/**
 * Gibt das oberste PhysicalVolume (worldVolume) zurueck.
 * Achtung: Keine Fehlerbehebung! Falls Construct() noch nicht aufgerufen
 * wurde und somit das worldVolume noch nicht initialisiert wird, wird
 * dies nicht bemerkt.
 */
G4VPhysicalVolume* StrahlentherapieDetectorConstruction::GetPhysicalVolume()
{
	return worldVolume_phys;
}

